package com.sakura.mall.order.orderReturnApply.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.order.orderReturnApply.domain.entity.OrderReturnApplyEntity;
import com.sakura.mall.order.orderReturnApply.domain.request.AddOrderReturnApplyRequest;
import com.sakura.mall.order.orderReturnApply.domain.request.UpdateOrderReturnApplyRequest;
import com.sakura.mall.order.orderReturnApply.domain.request.OrderReturnApplyPageRequest;
import com.sakura.mall.order.orderReturnApply.domain.vo.OrderReturnApplyVo;
import com.sakura.mall.order.orderReturnApply.mapper.OrderReturnApplyMapper;
import com.sakura.mall.order.orderReturnApply.service.OrderReturnApplyService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单退货申请 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class OrderReturnApplyServiceImpl extends ServiceImpl<OrderReturnApplyMapper, OrderReturnApplyEntity> implements OrderReturnApplyService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddOrderReturnApplyRequest request) {
        // todo 其他校验逻辑
        OrderReturnApplyEntity orderReturnApply = request.convert(OrderReturnApplyEntity.class);
        this.save(orderReturnApply);
    }

    @Override
    public Page<OrderReturnApplyVo> queryPage(OrderReturnApplyPageRequest request) {
        IPage<OrderReturnApplyEntity> qp = request.toPage();
        LambdaQueryWrapper<OrderReturnApplyEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<OrderReturnApplyVo> ret = qp.convert(entity -> entity.convert(OrderReturnApplyVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateOrderReturnApplyRequest request) {
        OrderReturnApplyEntity entity = request.convert(OrderReturnApplyEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

